package cn.exrick.xboot.modules.assessment.serviceimpl;

import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.assessment.dao.mapper.HkActTypeMapper;
import cn.exrick.xboot.modules.assessment.entity.HkActType;
import cn.exrick.xboot.modules.assessment.entity.HkIndexClassify;
import cn.exrick.xboot.modules.assessment.service.IHkActTypeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yck
 * @since 2022-09-02
 */
@Service
@Transactional
public class HkActTypeServiceImpl extends ServiceImpl<HkActTypeMapper, HkActType> implements IHkActTypeService {


    @Autowired
    private HkActTypeMapper hkActTypeMapper;

    @Override
    public IPage<HkActType> selectPage(Page<HkActType> page, HkActType hkActType) {
        QueryWrapper<HkActType> query = Wrappers.query();
        query.lambda().like(StringUtils.isNotBlank(hkActType.getActTypeName()),HkActType::getActTypeName,hkActType.getActTypeName());
        QueryWrapper<HkActType> entityWrapper = getEntityWrapper(query, hkActType);
        return hkActTypeMapper.selectPage(page, entityWrapper);
    }


    @Override
    public List<HkActType> selectList(HkActType hkActType) {
        QueryWrapper<HkActType> query = Wrappers.query();
        QueryWrapper<HkActType> entityWrapper = getEntityWrapper(query, hkActType);
        return hkActTypeMapper.selectList(entityWrapper);
    }

    /**
     * 公共查询条件
     *
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<HkActType> getEntityWrapper(QueryWrapper<HkActType> entityWrapper, HkActType hkActType) {

        return entityWrapper;
    }

    @Override
    public Result<Object> saveHkActType(String actTypeName){
        QueryWrapper<HkActType> query = Wrappers.query();
        query.lambda().eq(StringUtils.isNotBlank(actTypeName),HkActType::getActTypeName,actTypeName);
        HkActType hkActTypeByName = hkActTypeMapper.selectOne(query);
        if (hkActTypeByName != null){
            return ResultUtil.error("该类型已存在");
        }
        HkActType hkActType = new HkActType();
        hkActType.setActTypeName(actTypeName);
        hkActTypeMapper.insert(hkActType);
        return ResultUtil.success("添加成功");
    }
}
